package cn.tedu.gateway.ip;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
public class RedisIpListService {
    //定义一个黑名单的key
    private static final String BLACK_LIST_KEY = "ip:blacklist";
    //定义一个白名单的key
    private static final String WHITE_LIST_KEY = "ip:whitelist";

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //获取黑名单ip的方法
    public Set<String> getBlackList() {
        return stringRedisTemplate.opsForSet().members(BLACK_LIST_KEY);
    }

    //获取白名单ip的方法
    public Set<String> getWhiteList() {
        return stringRedisTemplate.opsForSet().members(WHITE_LIST_KEY);
    }

    //添加黑名单ip的方法
    public void addToBlackList(String ip) {
        stringRedisTemplate.opsForSet().add(BLACK_LIST_KEY, ip);
    }

    //添加白名单ip的方法
    public void addToWhiteList(String ip) {
        stringRedisTemplate.opsForSet().add(WHITE_LIST_KEY, ip);
    }

    //移除黑名单ip的方法
    public void removeFromBlackList(String ip) {
        stringRedisTemplate.opsForSet().remove(BLACK_LIST_KEY, ip);
    }

    //移除白名单ip的方法
    public void removeFromWhiteList(String ip) {
        stringRedisTemplate.opsForSet().remove(WHITE_LIST_KEY, ip);
    }
}
